//
//  JRAccountModel.swift
//  myWeibo
//
//  Created by 樊健荣 on 16/7/21.
//  Copyright © 2016年 樊健荣. All rights reserved.
//

import UIKit

class JRAccountModel: NSObject,NSCoding {
    
    /********************************模型属性*********************************/
    ///用户授权的唯一票据
    var access_token: String?
    
    ///	access_token的生命周期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    /// 方便比较日期的date
    var expiresDate: NSDate?
    
    ///授权用户的UID
    var uid: String?
    
    /********************************Method*********************************/
    
    ///构造创建函数
    init(dic: [String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dic)
        
    }
    ///在字典内的元素没有完全赋值给模型的时候,实现这函数,猜不会崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    ///打印时候调用,返回自定义内容
    override var description: String {
        get{
            return "access_token: \(access_token), expires_in: \(expires_in), uid: \(uid), expiresDate: \(expiresDate)"
        }
    }
    
    // MARK: - 编码与解码
    ///解码
    required init?(coder aDecoder: NSCoder) {
        print("\(NSHomeDirectory())")
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeObjectForKey("expires_in") as! NSTimeInterval
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
        
        
    }
    ///编码
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    
    
}


